#include <cstdio>
#include <memory.h>
#include <queue>
#include <iostream>
using namespace std;

struct node {
	int x, w;
	node(int v, int wi) {x = v; w = wi;}
};

int dis[100005];
bool vis[100005];
queue<int> q;
vector<node> G[100005];

void spfa() {
	memset(dis, 0x3f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	dis[1] = 0;
	vis[1] = 1;
	q.push(1);
	while (q.size()) {
		int x = q.front();
		q.pop();
		//cout << "break point 2 at x = " << x << endl;
		vis[x] = 0;
		for (unsigned i = 0; i < G[x].size(); ++i) {
			int y = G[x][i].x, z = G[x][i].w;
			if (dis[y] > dis[x] + z) {
				dis[y] = dis[x] + z;
				if (!vis[y]) {
					q.push(y);
					vis[y] = 1;
				}
			}
		}
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	int u, v, w;
	for (int i = 1; i <= m; ++i) {
		cin >> u >> v >> w;
		G[u].push_back(node(v, w));
	}

	//cout << "break point 1." << endl;

	spfa();

	printf("%d\n", dis[n]);
	return 0;
}